//
//  MemberExchangeGiftViewController.m
//  P001
//
//  Created by panquanReabam.com on 16/6/3.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "MemberExchangeGiftViewController.h"
#import "HttpRequest.h"
#import "MemberfindIntegralListViewController.h"
@interface MemberExchangeGiftViewController  ()<UITextFieldDelegate,UIGestureRecognizerDelegate ,UIScrollViewDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate>
@end

@implementation MemberExchangeGiftViewController
{
    UITextField* priceTextFied;
    UITextField* contentTextFied;
    UIButton*registdd;
    BOOL isHaveDian;
}
@synthesize alt = alt;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"会员积分";
    // 手势
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapDone)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate = self;
    [self.view addGestureRecognizer:singleFingerOne];
    
    UIView*myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
    myToolBar.backgroundColor=jianpancolor;
    UIButton*  tubtnDone=[[UIButton alloc]initWithFrame:CGRectMake(logoX,logoY, logoheight, logowidth)];
    [tubtnDone setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [tubtnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:tubtnDone];
    UIButton*btnDone111=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
    [btnDone111 setTitle:@"确定" forState:UIControlStateNormal];
    [btnDone111 setTitleColor:backColor forState:UIControlStateNormal];
    btnDone111 .titleLabel .font=[UIFont systemFontOfSize:keyboardFot];
    [btnDone111 addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:btnDone111];
    myToolBar.alpha=alp;
    UIButton* dabtn=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width-60, 0, 60, 40)];
    [dabtn addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:dabtn];
    
    
    UIView*heardView=[[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, 130)];
    heardView.backgroundColor=luseColor;
    [self.view addSubview:heardView];
    UIView*heardView1=[[UIView alloc]initWithFrame:CGRectMake(0, 120, ScreenWidth, 10)];
    heardView1.backgroundColor=zuiqianhuiColor;
    heardView1.layer.borderWidth = 0.40;
    heardView1.layer.borderColor = qianhuiColor.CGColor;
    [heardView addSubview:heardView1];

    
    UILabel* priceLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 35, ScreenWidth,32)];
    priceLabel.text=[NSString stringWithFormat:@"%@", self.price];
    priceLabel.font=[UIFont systemFontOfSize:32];
    priceLabel.textAlignment=NSTextAlignmentCenter;
    priceLabel.textColor=[UIColor whiteColor];
    [heardView addSubview:priceLabel];
    
    UIButton* ToObtain = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth/2-45, priceLabel.bottom+12, 90,26)];
    [ToObtain setTitle:@"可用积分" forState:UIControlStateNormal];
    ToObtain.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    [ToObtain.layer setCornerRadius:13]; //设置矩圆角半径
    [ToObtain.layer setBorderWidth:0.5];   //边框宽度
    CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){255/255.0, 255/255.0, 255/255.0, 0.6});
    [ToObtain.layer setBorderColor:tureBTncolorref];//边框颜色
    [ToObtain setTitleColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1]
                   forState:UIControlStateNormal];
    [ToObtain addTarget:self action:@selector(ToObtain:) forControlEvents:UIControlEventTouchUpInside];
    [heardView addSubview:ToObtain];


    UILabel* price=[[UILabel alloc]initWithFrame:CGRectMake(20, heardView.bottom+20, 70,44)];
    price.text=[NSString stringWithFormat:@"%@", @"兑现积分"];
    price.font=[UIFont systemFontOfSize:15];
    price.textColor=shenhuiColor;
    [self.view addSubview:price];
    priceTextFied=[[UITextField alloc]initWithFrame:CGRectMake(price.right, heardView.bottom+20, ScreenWidth-105, 44)];
    priceTextFied.borderStyle = UITextBorderStyleNone;
    priceTextFied.font = [UIFont systemFontOfSize:15];
    priceTextFied.textColor=[UIColor blackColor];
    priceTextFied.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    priceTextFied.tag=1;
    priceTextFied.delegate=self;
    priceTextFied.keyboardType = UIKeyboardTypeNumberPad;
    priceTextFied.placeholder = @"请输入要兑现的金额"; //默认显示的字
    [priceTextFied setUserInteractionEnabled:YES];
    priceTextFied.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:priceTextFied];
    priceTextFied.inputAccessoryView = myToolBar;
    UILabel* content=[[UILabel alloc]initWithFrame:CGRectMake(20,   priceTextFied.bottom, 70,44)];
    content.text=[NSString stringWithFormat:@"%@", @"兑现内容"];
    content.font=[UIFont systemFontOfSize:15];
    content.textColor=shenhuiColor;
    [self.view addSubview:content];
    
   contentTextFied=[[UITextField alloc]initWithFrame:CGRectMake(price.right, priceTextFied.bottom, ScreenWidth-105, 44)];
    contentTextFied.borderStyle = UITextBorderStyleNone;
    contentTextFied.font = [UIFont systemFontOfSize:15];
    contentTextFied.textColor=[UIColor blackColor];
    contentTextFied.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    contentTextFied.tag=1;
    contentTextFied.placeholder = @"请输入要兑现的内容"; //默认显示的字
    [contentTextFied setUserInteractionEnabled:YES];
    contentTextFied.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:contentTextFied];
    contentTextFied.inputAccessoryView = myToolBar;

    
    UILabel*lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(10,priceTextFied.bottom, ScreenWidth-20, 0.40)];
    [lineLabel setBackgroundColor:qianhuiColor];
    [self.view addSubview:lineLabel];
    UILabel*lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(10,contentTextFied.bottom, ScreenWidth-20, 0.40)];
    [lineLabel1 setBackgroundColor:qianhuiColor];
    [self.view addSubview:lineLabel1];
    registdd = [[UIButton alloc] initWithFrame:CGRectMake(0,ScreenHeight-48, ScreenWidth, 48)];
    [registdd setTitle:@"确定" forState:UIControlStateNormal];
    [registdd setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    registdd.backgroundColor=luseColor;
    [registdd addTarget:self action:@selector(regist:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registdd];
}


-(void)ToObtain:(UIButton*)sender{
    MemberfindIntegralListViewController * Login=[[MemberfindIntegralListViewController alloc]init];
    Login.memberId=self.memberId;
    [self.navigationController pushViewController:Login animated:YES];
}
-(void)regist:(UIButton*)sender{
    
    
    if ([priceTextFied.text length]<1)
    {
        [self showAlertMessage:@"请输入兑现的金额"];
    }
    else{
        
        [Global showIndeterminateHud:@"请稍后。。。" delay:20 view:self.view];
        NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
        NSString* tokenId = [user1 objectForKey:@"tokenId"];
        NSMutableDictionary *params = [NSMutableDictionary new];
        [params setValue:tokenId forKey:@"tokenId"];
         [params setValue:self.memberId forKey:@"memberId"];
        [params setValue:priceTextFied.text forKey:@"needIntegral"];
        [params setValue:contentTextFied.text forKey:@"content"];
        NSLog(@"====%@",params);
        [HttpRequest requestData:@"Business/Member/ExchangeGift" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
//                NSLog(@"====%@",severDataDict.dataDict);
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
                    [_delegate update:@""];
                    
                   [self.navigationController popViewControllerAnimated:YES];
                    
                }else{
                    [Global hideProgressHUDInternal:self.view];
                    [self showAlertMessage:[severDataDict.dataDict objectForKey:@"ResultString"]];
                    
                }
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                
                [Global showTextHudSub:severDataDict.resultStr view:self.view];
            }
        }];
        
    }
}

- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定哦 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}


- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:2.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)OnTapDone{
    
    [contentTextFied resignFirstResponder];
     [priceTextFied resignFirstResponder];
    
  
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    if (textField.tag == 1)
    {
        
        if (range.location >= 9){
            return NO;
        }
        if ([textField.text rangeOfString:@"."].location==NSNotFound) {
            isHaveDian=NO;
        }
        if ([string length]>0)
        {
            unichar single=[string characterAtIndex:0];//当前输入的字符
            if ((single >='0' && single<='9') || single=='.')//数据格式正确
            {
                //首字母不能为0和小数点
                if([textField.text length]==0){
                    if(single == '.'){
                        
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                        
                    }
                    if (single == '0') {
                        // [self alertView:@"亲，第一个数字不能为0"];
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                        
                    }
                }
                if (single=='.')
                {
                    if(!isHaveDian)//text中还没有小数点
                    {
                        isHaveDian=YES;
                        return YES;
                    }else
                    {
                        //[self alertView:@"亲，您已经输入过小数点了"];
                        [textField.text stringByReplacingCharactersInRange:range withString:@""];
                        return NO;
                    }
                }
                else
                {
                    if (isHaveDian)//存在小数点
                    {
                        //判断小数点的位数
                        NSRange ran=[textField.text rangeOfString:@"."];
                        int tt=range.location-ran.location;
                        if (tt <= 2){
                            return YES;
                        }else{
                            //[self alertView:@"亲，您最多输入两位小数"];
                            return NO;
                        }
                    }
                    else
                    {
                        return YES;
                    }
                }
            }else{//输入的数据格式不正确
                //[self alertView:@"亲，您输入的格式不正确"];
                [textField.text stringByReplacingCharactersInRange:range withString:@""];
                return NO;
            }
        }
        
        
        
    }
    

    
    return YES;
    
    
    
    
    
}


@end
